#include "loadtxt.h"

#define N 500
FILE *infp = NULL, *outfp = NULL;
char buf[N] = {0.0};
// double dataIn[20] = {0.0};

int lineNum = 0;
#pragma pack(push,4)
struct dataIn_s
{
    double k[14];
} dataIn;
#pragma pack(pop)

void loadInTxt(const char *path)
{
    infp = fopen(path, "r+");
}

void closeInTxt()
{
    fclose(infp);
}

int readLine(void)
{
    if (fgets(buf, N, infp) != NULL)
    {
        lineNum++;
        // sscanf(buf, "%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e,%e",
        //        dataIn, dataIn + 1, dataIn + 2, dataIn + 3, dataIn + 4, dataIn + 5, dataIn + 6, dataIn + 7, dataIn + 8, dataIn + 9,
        //        dataIn + 10, dataIn + 11, dataIn + 12, dataIn + 13, dataIn + 14,dataIn + 15,dataIn + 16,dataIn + 17,dataIn + 18,dataIn + 19);

        sscanf(buf, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf\r\n", 
                        dataIn.k, dataIn.k + 1, dataIn.k + 2, dataIn.k + 3, dataIn.k + 4, dataIn.k + 5, dataIn.k + 6,   // imu
                            dataIn.k + 7, dataIn.k + 8, dataIn.k + 9, dataIn.k + 10, dataIn.k + 11, dataIn.k + 12, dataIn.k + 13); //gnss
        return 1;
    }
    else
    {
        closeInTxt();
        return 0;
    }
}

void creatOutTxt(const char *path)
{
    outfp = fopen(path, "w+");
}

void closeOutTxt(void)
{
    fclose(outfp);
    printf("文件输出完成...\n");
}
void outLine(double *inBuf, int length)
{
    // float tmp = 11.0;
    if (inBuf != NULL)
    {
        for (int i = 1; i < length; i++)
        {
            fprintf(outfp, "%g,", *inBuf++);
        }
        fprintf(outfp, "%g\n", *inBuf);
    }
    else
    {
        closeOutTxt();
    }
}